﻿#include "MeasureView/MeasureCount.h"
#include "MeasureView/MeasureDataSet.h"
#include <QDebug>
namespace Qly {

MeasureCount::MeasureCount(MeasureDataSet *parent)
    :MeasureElement(parent)
{
    m_results_um.resize(1);
    m_results_pix.resize(1);
    m_measureType = Count;
    m_measureState = DS_IDLE;
}

void MeasureCount::compute()
{

}

void MeasureCount::updateText(bool updatePosition )
{
    Q_UNUSED(updatePosition);
}

int MeasureCount::mouseLButtonPress(QPointF point_pix, QPointF point_um)
{
    if( m_measureState == DS_IDLE )
    {
        m_keyPoints_pix.push_back(point_pix);
        m_keyPoints_um.push_back(point_um);
        m_measureState = DS_STATE1;
    }
    else if(m_measureState == DS_STATE1)
    {
        m_keyPoints_pix.push_back(point_pix);
        m_keyPoints_um.push_back(point_um);
    }
    return m_measureState;
}

int MeasureCount::mouseRButtonPress(QPointF point_pix, QPointF point_um)
{
    if(m_measureState == DS_STATE1)
    {
        m_keyPoints_pix.push_back(point_pix);
        m_keyPoints_um.push_back(point_um);

        m_results_um[0] = m_keyPoints_um.size();
        m_results_pix[0] = m_keyPoints_pix.size();
        m_measureState = DS_FINISH;
    }
    return m_measureState;
}

MeasureElement::DeleteResult MeasureCount::tryDelete(QPointF p)
{
    int N = m_keyPoints_pix.size();
    for (int i = 0; i < N; ++i)
    {
        //QPointF t = m_keyPoints_pix[i] - p;

        QString str = QString::number(i + 1);
        QFontMetrics fm(m_font);
        QRect rect = fm.boundingRect(str);
        rect.moveBottomLeft(m_keyPoints_pix[i].toPoint());

        // qDebug() << t.manhattanLength();
        if(rect.contains(p.toPoint()))
        {
            m_keyPoints_pix.removeAt(i);
            m_keyPoints_um.removeAt(i);
            if(N == 1)
            {
                return MeasureElement::DeleteByOther;
            }
            return MeasureElement::DeleteByMyself;
        }
    }
    return MeasureElement::DeleteNothing;
}

void MeasureCount::paint(QPainter &painter)
{
    if( m_measureState != DS_IDLE  || m_measureState != DS_ERROR)
    {
        painter.save();
        painter.setPen(m_pen);
        painter.setFont(m_font);
        int N = m_keyPoints_pix.size();
        for (int i = 0; i < N; ++i)
        {
            QString str = QString::number(i + 1);
            QFontMetrics fm(m_font);
            QRect rect = fm.boundingRect(str);
            rect.moveBottomLeft(m_keyPoints_pix[i].toPoint());
            painter.fillRect(rect, Qt::white);
            painter.drawText(m_keyPoints_pix[i], str);
        }
        painter.restore();
    }
}

//int MeasureCount::setLens(int oldLens, int newLens, int method)
//{
//    if(oldLens == newLens || oldLens == -1) return 0;// 这时不需要更新任何数据
//    if(m_keyPoints_pix.size() != m_keyPoints_um.size()) //这里表示出现了错误，也应该退出
//    {
//        return 0;
//    }
//    LensManager & lens = LensManager::getInstance();
//    if(method == 0)//只更新长度的数值，图元在图像上的位置不变
//    {
//        double new_pixelSize = lens.currentLens().pixelSize_um;
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_um[i] = m_keyPoints_pix[i] * new_pixelSize;
//        }
//    }
//    else
//    {
//        LensData & oldData = lens.lensByIndex(oldLens);
//        LensData & newData = lens.currentLens();
//        double dx = (newData.xCompensate_um - oldData.xCompensate_um);
//        double dy = (newData.yCompensate_um - oldData.yCompensate_um);
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_pix[i] = (m_keyPoints_um[i] + QPointF(dx, dy)) / newData.pixelSize_um;
//        }
//    }
////    compute();
////    updateText();
//    return 1;
//}
}
